import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/handle/xindong_user_handle.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_hello_ctr.dart';
import 'package:xindong/manager/xindong_reset_singleton.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/models/user/xindong_home_user.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/common/xindong_setting_general_yemian.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_button.dart';
import 'package:xindong/widgets/dialog/xindong_base_alert.dart';
import 'package:xindong/widgets/xindong_avatar.dart';

class XinDongTodayFateDialog extends StatefulWidget {
  List<XinDongHomeUser> dataList = [];

  XinDongTodayFateDialog({super.key});

  XinDongTodayFateDialog.show({super.key}) {
    if (XinDongDailyResetSingleton.instance.canShowTodayFate &&
        XinDongUserController.to.userInfo?.configPersonal?.fate_switch == 1) {
      XinDongDailyResetSingleton.instance.canShowTodayFate = false;
      XinDongUserHandle().requestHomeUserList(
          yemian: 0,
          fate: 1,
          callback: (object) {
            if (object.code == XinDongServerStatus.success) {
              List list = object.result;
              dataList = list.map((e) => XinDongHomeUser.fromJson(e)).toList();
              showAlertDialog(alert: this);
            }
          });
    }
  }

  @override
  State<XinDongTodayFateDialog> createState() => _XinDongTodayFateDialogState();
}

class _XinDongTodayFateDialogState extends State<XinDongTodayFateDialog> {
  List<XinDongHomeUser> selectUsers = [];

  @override
  void initState() {
    super.initState();

    selectUsers = List.from(widget.dataList);
  }

  @override
  Widget build(BuildContext context) {
    return XinDongBaseDialog(
      body: Container(
        width: 300,
        decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(16)),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Stack(
              clipBehavior: Clip.none,
              fit: StackFit.loose,
              children: [
                SizedBox(
                  height: 40,
                  child: OverflowBox(
                    maxHeight: 150,
                    alignment: Alignment.bottomCenter,
                    child: ImageUtil.asset(AssetsRes.IMAGE_HELLO_TOP, width: 232, height: 80),
                  ),
                ),
                Positioned(
                  left: 0,
                  top: 3,
                  child: IconButton(
                    icon: const Icon(Icons.settings, color: DZColors.placeholderColor),
                    iconSize: 26,
                    onPressed: () => XinDongPageSingleton.instance.pushYeMian(const XinDongSettingGeneralYeMian()),
                  ),
                ),
                Positioned(
                  right: 0,
                  top: 3,
                  child: IconButton(
                    icon: const Icon(Icons.close, color: DZColors.placeholderColor),
                    iconSize: 26,
                    onPressed: () => Get.back(),
                  ),
                ),
              ],
            ),
            GridView.builder(
              itemCount: widget.dataList.length,
              padding: const EdgeInsets.all(20),
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                childAspectRatio: 0.85,
                crossAxisCount: 3,
                crossAxisSpacing: 10,
                mainAxisSpacing: 16,
              ),
              itemBuilder: (BuildContext context, int index) {
                final user = widget.dataList[index];
                final isSelect = selectUsers.any((element) => element.uid == user.uid);
                return GestureDetector(
                  onTap: () {
                    if (isSelect) {
                      selectUsers.removeWhere((element) => element.uid == user.uid);
                    } else {
                      selectUsers.add(user);
                    }
                    setState(() {});
                  },
                  child: Stack(
                    alignment: Alignment.center,
                    clipBehavior: Clip.none,
                    children: [
                      Positioned(
                        top: 0,
                        child: XinDongAvatar(url: user.user_head, size: 66, radius: 8),
                      ),
                      Positioned(
                        bottom: 0,
                        child: Text(
                          "${user.user_name}",
                          style: const TextStyle(fontSize: 14, color: DZColors.contentColor),
                        ),
                      ),
                      if (isSelect)
                        Positioned(
                          top: -6,
                          right: 0,
                          child: ImageUtil.asset(AssetsRes.ICON_CHECKED, width: 16, height: 16),
                        ),
                    ],
                  ),
                );
              },
            ),
            Container(
              height: kButtonHeight,
              margin: const EdgeInsets.only(left: 20, right: 20, bottom: 20),
              child: XinDongCommonButton("搭讪", onTap: () {
                if (selectUsers.isEmpty) {
                  showToast("请选择搭讪用户");
                  return;
                }
                for (var user in selectUsers) {
                  XinDongHelloCtr.to.onSayHelloUser(user, free: true, needJumpToChat: false);
                }
                Get.back();
              }),
            ),
          ],
        ),
      ),
    );
  }
}
